package anthology.daily_question._24_10;
//https://leetcode.cn/problems/lexicographically-smallest-string-after-a-swap/description/?envType=daily-question&envId=2024-10-30
//拓展：670. 最大交换 https://leetcode.cn/problems/maximum-swap/description/
public class _3216交换后字典序最小的字符串 {
    //枚举
    class Solution {
        public String getSmallestString(String s) {
            char[] arr = s.toCharArray();
            for (int i = 0; i + 1 < arr.length; i++) {
                if (arr[i] > arr[i + 1] && arr[i] % 2 == arr[i + 1] % 2) {
                    char temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;
                    break;
                }
            }
            return new String(arr);
        }
    }
    //灵神
    class Solution2 {
        public String getSmallestString(String s) {
            char[] t = s.toCharArray();
            for (int i = 1; i < t.length; i++) {
                char x = t[i - 1];
                char y = t[i];
                if (x > y && x % 2 == y % 2) {
                    t[i - 1] = y;
                    t[i] = x;
                    break;
                }
            }
            return new String(t);
        }
    }
}

